package es.ats.codington.festival.services;

import java.sql.Connection;

import java.sql.SQLException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import es.ats.codington.festival.daos.ManagerDAOImp;
import es.ats.codington.festival.daos.VisitorDAOImp;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.interfaces.daos.IVisitorDAOImp;
import es.ats.codington.festival.interfaces.services.IVisitorService;
import es.ats.codington.festival.utils.FERSDataConnection;

public class VisitorService implements IVisitorService{

	int nRows = 0;
	private Connection con;
	IVisitorDAOImp visitorDAO;
	
	
	
	public VisitorService() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
		ManagerDAOImp managerDAO = (ManagerDAOImp) ctx.getBean("daoManager");
		this.visitorDAO = managerDAO.getVisitorDAO();
	}
	/**
	 * Methods for VisitorService class that checks whether the user is correct
	 * @return
	 */
	public int loginDAOService(String name, String pass) {
			Visitor visitor= new Visitor();
			visitor.setUserName(name);
			visitor.setPassword(pass);
		return  visitorDAO.loginDAO(visitor);
	}
	/**
	 * SETTER methods for VisitorService class 
	 * @return
	 */
	public int insertVisitorService(Visitor visitor){
		return visitorDAO.insertVisitor(visitor);
	}
	/**
	 * SETTER methods for VisitorService class 
	 * @return
	 */
	public int updateVisitorSevice(Visitor uVisitor , String userName){
		return visitorDAO.updateVisitor(uVisitor,userName);
	}
//	/**
//	 * SETTER methods for VisitorService class 
//	 * @return
//	 */
//	public int deleteVisitorService(String visitor){
//		try{
//			con = FERSDataConnection.getConnection();
//			nRows = visitorDAO.deleteVisitor(visitor, con);
//			con.close();
//		}catch (SQLException e){
//			System.out.println(e.getMessage());
//		}
//		return nRows;
//	}
	/**
	 * SETTER methods for VisitorService class 
	 * @return
	 */
	public int updatePasswordService(String newPassword, String oldPassword, String userName) {
		
		return visitorDAO.updatePassword(newPassword, oldPassword, userName);
		
	
	}
	/**
	 * GETTER methods for VisitorService class 
	 * @return
	 */
	public int getVisitorIdService(String userName) {
		try{
			con = FERSDataConnection.getConnection();
			nRows = visitorDAO.getVisitorId(userName, con);
			con.close();
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		return nRows;
	}
	/**
	 * GETTER methods for VisitorService class 
	 * @return
	 */
	public int isAdminService (String userName) {
		int resultado=0;
		try{
			con = FERSDataConnection.getConnection();
			resultado = visitorDAO.isAdmin(userName, con);
			con.close();
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		return resultado;
	}
	public Visitor getVisitorService(String userName) {
		Visitor visitor = new Visitor();
		try{
			con = FERSDataConnection.getConnection();
			visitor = visitorDAO.getVisitor(userName, con);
			con.close();
		}catch (SQLException e){
			System.out.println(e.getMessage());
		}
		return visitor;
	}
}
